package com.zkingsoft.laqeelin.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.ModelUtils;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.UUIDUtil;
import com.matrix.core.tools.WebUtil;
import com.zkingsoft.common.bean.SysUsers;
import com.zkingsoft.common.constance.AppConstance;
import com.zkingsoft.laqeelin.bean.BizHelpActivity;
import com.zkingsoft.laqeelin.dao.BizHelpActivityDao;
import com.zkingsoft.laqeelin.service.BizHelpActivityService;
import com.zkingsoft.tools.WxacodeUtil;

/**
 * @description service接口实现类(助力活动表)
 * @author 何明
 * @date 2018-09-01 10:35
 */
@Service
public class  BizHelpActivityServiceImpl implements  BizHelpActivityService{

	
	@Autowired
	private BizHelpActivityDao bizHelpActivityDao;
	
	
	@Override
	public int add(BizHelpActivity bizHelpActivity){
		// 设置基本字段信息
		SysUsers loginUser = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		bizHelpActivity.setCreateBy(loginUser.getSuName());
		bizHelpActivity.setUpdateBy(loginUser.getSuName());
		bizHelpActivity.setHacId(UUIDUtil.getRandomID());
	
		if(bizHelpActivity.getHacStatus()==null){
			bizHelpActivity.setHacStatus(AppConstance.ACT_STATUS_BC);
		}
		if(bizHelpActivity.getBizHelpPrize()!=null){
			bizHelpActivity.setHelpPrize(JSONObject.toJSONString(bizHelpActivity.getBizHelpPrize()));
		}
		if(bizHelpActivity.getBizSharePrize()!=null){
			bizHelpActivity.setSharePrize(JSONObject.toJSONString(bizHelpActivity.getBizSharePrize()));
		}
		if(bizHelpActivity.getBizCodePosition()!=null){
			bizHelpActivity.setCodePosition(JSONObject.toJSONString(bizHelpActivity.getBizCodePosition()));
		}
		int i=bizHelpActivityDao.insert(bizHelpActivity);
		return i;
		
	}
	
	@Override
	public int batchAdd(List<BizHelpActivity>  bizHelpActivityList) {
		//这里没有做基本字段的设置，如有需要请自己实现	
		int num = 0;
		int c = 10000;
		int size = bizHelpActivityList.size()/c + 1;
		for(int i=0; i<size; i++) {
			int begin = i*c;
			int end = (i+1)*c;
			end = end >= bizHelpActivityList.size() ? bizHelpActivityList.size() : end;
			List<BizHelpActivity> insertList = bizHelpActivityList.subList(begin, end);
			num += bizHelpActivityDao.batchInsert(insertList);
		}
		return num;
		
	}
	
	
   	
    @Override
	public int modifyByMap(BizHelpActivity oldBizHelpActivity
	,BizHelpActivity newBizHelpActivity){
	
		Map<String, Object> modifyMap = null;
		try {
			if (!ModelUtils.isModified(oldBizHelpActivity, newBizHelpActivity)) {
				return MatrixConstance.DML_SUCCESSS;
			}
			if(newBizHelpActivity.getHacStatus()==null){
				newBizHelpActivity.setHacStatus(AppConstance.ACT_STATUS_BC);
			}
			modifyMap = ModelUtils.comparePojo2Map(oldBizHelpActivity, newBizHelpActivity);
		} catch (Exception e) {
			throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL, e, newBizHelpActivity);
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("hacId", oldBizHelpActivity.getHacId());
			bizHelpActivityDao.updateByMap(modifyMap);
			
		}
		return MatrixConstance.DML_SUCCESSS;
	}
	
	@Override
	public int modifyByModel(BizHelpActivity bizHelpActivity){
	
		if(bizHelpActivity.getBizHelpPrize()!=null){
			bizHelpActivity.setHelpPrize(JSONObject.toJSONString(bizHelpActivity.getBizHelpPrize()));
		}
		if(bizHelpActivity.getBizSharePrize()!=null){
			bizHelpActivity.setSharePrize(JSONObject.toJSONString(bizHelpActivity.getBizSharePrize()));
		}
		if(bizHelpActivity.getBizCodePosition()!=null){
			bizHelpActivity.setCodePosition(JSONObject.toJSONString(bizHelpActivity.getBizCodePosition()));
		}
		return bizHelpActivityDao.updateByModel(bizHelpActivity);
	
	}
	
	
	
	@Override
	public int remove(List<String> list){
	
		return bizHelpActivityDao.deleteByIds(list);
	
	}

	@Override
	public int removeById(String actId){
	
		return bizHelpActivityDao.deleteById(actId);
	
	}
	
	@Override
	public int removeByModel(BizHelpActivity bizHelpActivity){
	
		return bizHelpActivityDao.deleteByModel(bizHelpActivity);
	
	}
	
	
	@Override
	public List<BizHelpActivity> findInPage(BizHelpActivity bizHelpActivity,  PaginationVO pageVo){
	
		return bizHelpActivityDao.selectInPage(bizHelpActivity , pageVo);
	
	}
	
	@Override
	public List<BizHelpActivity> findByModel(BizHelpActivity bizHelpActivity){
	
		return bizHelpActivityDao.selectByModel(bizHelpActivity);
	
	}
	
	@Override
	public int  findTotal(BizHelpActivity bizHelpActivity){
	
		return bizHelpActivityDao.selectTotalRecord(bizHelpActivity);
	
	}
	
	@Override
	public BizHelpActivity  findById(String actId){
	
		return bizHelpActivityDao.selectById(actId);
	
	}



	

   	
	
	
}